package com.example.controller;

import com.example.entity.Notice;
import com.example.entity.RespBean;
import com.example.mapper.NoticeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 有关发布公告controller
 *
 * @author: liu
 * 2022-04-08-11:05
 **/
@Api(tags = "NoticeController")
@RestController
@ResponseBody
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @ApiOperation(value = "查询所有公告")
    @GetMapping(value = "/query-all-notices")
    public List<Notice> queryAllNotices(){

        return noticeMapper.queryAllNotices();

    }

    @ApiOperation(value = "查询最新的一条公告")
    @GetMapping(value = "/query-first-notice")
    public List<Notice> queryAllNoticeOnlyOne(){

        return noticeMapper.queryAllNoticeOnlyOne();

    }

    @ApiOperation(value = "新增一条公告")
    @PostMapping(value = "/add-notice")
    public RespBean addNotice(@RequestBody Notice notice){

        noticeMapper.addNotice(notice.getNotice(),notice.getPublic_time(),notice.getRemark());
        return RespBean.success("新增成功");

    }

    @ApiOperation(value = "删除一条公告")
    @PostMapping(value = "/del-notice/{id}")
    public RespBean delNotice(@PathVariable int id){
        if(id != 0){
            noticeMapper.delNotice(id);
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }
}
